package cn.xfdzx.android.apps.shop.fragment.listing;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.ProcurementListingToPayAdapter;
import cn.xfdzx.android.apps.shop.adapter.SellingGoodsLeftLVAdapter;
import cn.xfdzx.android.apps.shop.adapter.TypeMainAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.bean.ListingToPayBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.ListingToPayContract;
import cn.xfdzx.android.apps.shop.contract.OneCategoryContract;
import cn.xfdzx.android.apps.shop.evenbus.ClassInitDataMessage;
import cn.xfdzx.android.apps.shop.presenter.ListingToPayPresenter;
import cn.xfdzx.android.apps.shop.presenter.OneCategoryPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：未支付
 */
public class ListingToPayFragment extends BaseMvpFragment<ListingToPayPresenter> implements ListingToPayContract.View {

    RecyclerView rvTypeGoods;
    private ProcurementListingToPayAdapter toPayAdapter;
    private Dialog dialog;//删除的确认弹出框
    private int now_item_position;//删除的
    private List<ListingToPayBeen.DataBean.PurchaseListBean> mList = new ArrayList<>();

    @Override
    protected void initView(View view) {
        rvTypeGoods = view.findViewById(R.id.rv_listing_topay);
//        EventBus.getDefault().register(this);
        mPresenter = new ListingToPayPresenter();
        mPresenter.attachView(this);


        dialog_delete();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvTypeGoods.setLayoutManager(linearLayoutManager);
        toPayAdapter = new ProcurementListingToPayAdapter(getActivity(), mList, mPresenter,false);
        toPayAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                dialog.show();
            }
        });
        rvTypeGoods.setAdapter(toPayAdapter);
        mPresenter.initData("1");
    }


    private void dialog_delete() {
        dialog = Utils.addDialog(getActivity(), R.layout.dialog, R.string.are_you_sure_to_delete, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mPresenter.purchaseDelete(mList.get(now_item_position).getPurchase_sn(), now_item_position);
                    dialog.dismiss();
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_listing_topay;
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(getActivity());
        }
    }


    @Override
    public void hideLoading() {

        if (ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().dismiss();
        }
    }

    //    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
//    public void XXX(ClassInitDataMessage messageEvent) {
//        if (messageEvent.getMessage()==1){
//
//        }
//    }
    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onInitSuccess(ListingToPayBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.clear();
            mList.addAll(bean.getData().getPurchase_list());
            toPayAdapter.notifyDataSetChanged();


        } else {
            Utils.toastMessage(getActivity(), bean.getMsg());
        }
    }

    @Override
    public void onPurchaseEditSuccess(PurchaseSubmitBeen bean, int position) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.get(position).setIs_update(false);
            mList.get(position).getGoods_list().clear();
            mList.get(position).setCategory_num(bean.getData().getCategory_num());
            mList.get(position).setGoods_num(bean.getData().getGoods_num());
            mList.get(position).getGoods_list().addAll(bean.getData().getGoods_list());
            toPayAdapter.notifyDataSetChanged();

        } else {
            Utils.toastMessage(getActivity(), bean.getMsg());
        }
    }

    @Override
    public void onPurchaseDeleteSuccess(BaseObjectBean bean, int position) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.remove(position);
            toPayAdapter.notifyDataSetChanged();

        } else {
            Utils.toastMessage(getActivity(), bean.getMsg());
        }

    }


    public static ListingToPayFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
        ListingToPayFragment fragment = new ListingToPayFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroy() {
//        if(EventBus.getDefault().isRegistered(this)){
//            EventBus.getDefault().unregister(this);
//        }
        super.onDestroy();
    }

}
